<?

JCAT_Package::ImportClass('JCAT_ViewGtk',JCAT::PACKAGE_MVCGTK) ;

class ViewGtkDlg extends JCAT_ViewGtk implements JCAT_IViewForm
{
	/**
	 * 控件基本信息
	 * 
	 * @access	public
	 * @param	$aParent=null	JCAT_ViewGtk		拥有此视图的对象
	 * @param	$aUI=null		JCAT_UIGtkGlade		模版引擎
	 * @return	void
	 */
	public function ViewGtkDlg( JCAT_IViewContainer $aParent, JCAT_UIGtkGlade $aUI=null )
	{
		$this->JCAT_ViewGtk('ViewGtkDlg','ViewGtkDlg','ViewGtkDlg',$aParent,$aUI) ;
		
		// 
		$this->AddView(new ViewBaseView($this)) ;

		// 创建 并 初始化 UICtrl ---------------------------
		$sUICtrlDir = dirname(__FILE__).'/' ;
		$this->AddUICtrl(JCAT_Package::ImportInstance('ViewClassName',$sUICtrlDir),'ViewClassName') ;
		$this->AddUICtrl(JCAT_Package::ImportInstance('UITemplatePath',$sUICtrlDir),'UITemplatePath') ;
		$this->AddUICtrl(JCAT_Package::ImportInstance('MainWidgetID',$sUICtrlDir),'MainWidgetID') ;
		$this->AddUICtrl(JCAT_Package::ImportInstance('InTemplateDir',$sUICtrlDir),'InTemplateDir') ;
	}
	
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBrowseGlade()
	{
		JCAT_Package::ImportClass('JCAT_GtkFSDialog',JCAT::PACKAGE_MVCGTK) ;
		
		$sGladePath = JCAT_GtkFSDialog::Choose(Gtk::FILE_CHOOSER_ACTION_OPEN,null,"Glade	*.glade") ;
		if($sGladePath===NULL)
		{
			return ;
		}
		
		$aUICtrl = $this->GetUICtrl('UITemplatePath') ;
		$aUICtrl->SetDataByExchange($sGladePath) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnGladeFileChanged(GtkEditable $wndEntry)
	{
		$sPath = $wndEntry->get_text() ;
		if( !is_file($sPath) )
		{
			return ;
		}
		
		$this->LoadGladeWidgetID($sPath) ;
	}
	
	public function OnCreateUICtrl()
	{
		$aUICtrl = $this->GetUICtrl('UITemplatePath') ;
		$sPath = $aUICtrl->GetExchangeData() ;
		
		$aCreator = $this->GetGladeWidgetCreator($sPath) ;
		if( !$aCreator )
		{
			return null ;
		}

		$aController = $this->GetParentController() ;
		$aTopController = $aController->GetTopController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTopController,'IToolboxCoder',JCAT_Language::Sentence('ViewGtkDlg类 只能在实现 IToolboxCoder 接口的控制器中使用。')) ;
		
		$sCurrentPath = $aTopController->GetPath() ;
		if( is_dir($sCurrentPath) )
		{
			$sDirPath =& $sCurrentPath ;
		}
		else 
		{
			$sDirPath = dirname($sCurrentPath).'/' ;
		}
		
		// 创建对话框对象
		$aDlg = new CreateUICtrlFromGladeDlg($sDirPath,$this) ;
		
		// 向窗体菜单添加内容
		$aIterator = $aCreator->CreateWidgetIdIterator() ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$sWidgetClass = $aIterator->Current() ;
			$sWidgetId = $aIterator->CurrentKey() ;
			
			if( key_exists($sWidgetClass,self::$arrWidgetCoderMapping) )
			{
				$aDlg->AddWidget($sWidgetId,$sWidgetClass,self::$arrWidgetCoderMapping[$sWidgetClass]) ;
			}
			
			$aIterator->Next() ;
		}
		
		// 创建、显示对话框窗口
		$aDlg->DoDialog() ;
		
		return null ;
	}

	/**
	 * what's this
	 * 
	 * @access	protected
	 * @param	$sGladePath		string			what's this
	 * @return	void
	 */
	protected function LoadGladeWidgetID($sGladePath)
	{
		$aCreator = $this->GetGladeWidgetCreator($sGladePath) ;
		JCAT_ASSERT::ASSERT_INSTANCE($aCreator,'JCAT_UIGtkWidgetCreator') ;
		
		// UICtrl 控件
		$aUICtrl = $this->GetUICtrl('MainWidgetID') ;
		
		$aIterator = $aCreator->CreateWidgetIdIterator() ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$wndWidgetClass = $aIterator->Current() ;
			$sWidgetId =  $aIterator->CurrentKey() ;

			$aUICtrl->AddOption("{$sWidgetId} ({$wndWidgetClass})",$sWidgetId) ;

			$aIterator->Next() ;
		}
	}

	protected function GetGladeWidgetCreator($sGladePath)
	{
		if( !is_file($sGladePath) )
		{
			return null ;
		}
		
		$aUI = new JCAT_UIGtkGlade() ;
		$sWidgetCreatorName = $aUI->LoadGtkWidgetCreatorClass($sGladePath) ;
		if(!$sWidgetCreatorName)
		{
			return null ;
		}
		
		JCAT_ASSERT::ASSERT_ISCLASS($sWidgetCreatorName,'JCAT_UIGtkWidgetCreator') ;
		
		return new $sWidgetCreatorName() ;
	}
	
	
	
	static public $arrWidgetCoderMapping = array(
		// 下拉菜单
		'GtkComboBox' => 'UICtrlSelect'
		, 'GtkComboBoxEntry' => 'UICtrlSelect'
		
		// 文本
		, 'GtkEntry' => 'UICtrlText'
		, 'GtkTextView' => 'UICtrlText'
		
		// 状态按钮
		, 'GtkCheckButton' => 'UICtrlGtkToggle'
		, 'GtkToggleButton' => 'UICtrlGtkToggle'
		, 'GtkRadioButton' => 'UICtrlGtkToggle'

		// 树型视图
		, 'GtkTreeView' => 'UICtrlGtkTree'
	) ;
}

?>